import {
    isNull,
    isNumber,
    isObject,
    isString,
    isArray,
    isSymbol
} from "@/unit/tool";

const columns = [{
        title: "单号",
        fixed: "left",
        width: "180px",
        dataIndex: "repairNumber",
        ellipsis: false
    },
    {
        title: "客户名称",
        dataIndex: "customerName",
        width: "180px",
        ellipsis: false
    },
    {
        title: "返修件类型",
        dataIndex: "productType",
        width: "180px",
        ellipsis: false
    },
    {
        title: "返修件型号",
        dataIndex: "productModel",
        width: "180px",
        ellipsis: false,
        class: "fault-description"
    },
    {
        title: "返修件编号",
        dataIndex: "productNumber",
        width: "180px",
        ellipsis: false
    },
    {
        title: "故障信息",
        dataIndex: "faultInformation",
        width: "180px",
        ellipsis: false
    },
    {
        title: "返修时间",
        dataIndex: "time",
        width: "180px",
        ellipsis: false
    },
    {
        title: "返修单状态",
        dataIndex: "status",
        // width: "180px",
        ellipsis: false,
        class: "bill-state",
        customRender: (text, record, index) => {
            return formatState(text);
        }
    },
    {
        title: "操作",
        dataIndex: "operateStatus",
        // width: "150px",
        ellipsis: true,
        scopedSlots: { customRender: "operateStatus" }
    }
];

function formatState(state) {
    switch (state) {
        case 2:
            return "返厂中";
        case 3:
            return "已收货";
        case 4:
            return "已指派";
        case 5:
            return "维修收货";
        case 6:
            return "维修中";
        case 8:
            return "维修完成";
        case 10:
            return "返修完成";
        case 11:
            return "维修中";
        default:
            break;
    }
}

function buildTableData(lists) {

    // let { afterSaleService, appointor, evaluation, repairList, suit } = res;
    let newLists = [];
    lists.forEach((item, index) => {
        newLists.push(buildListsData(item, index));
    });
    return newLists;
}

function buildListsData(item, index) {
    let newRes = { key: index };
    for (let key in item) {
        if (Object.hasOwnProperty.call(item, key)) {
            let obj = item[key];
            if (!isNull(obj)) {
                newRes = Object.assign(newRes, resolveParams(obj, key));
            }
        }
    }
    return newRes;
}

function resolveParams(val, key) {
    if (isNumber(val)) {
        return {
            [key]: val
        };
    } else if (isString(val)) {
        return {
            [key]: val
        };
    } else if (isArray(val)) {
        return {
            [key]: val
        };
    } else if (isObject(val)) {
        let { id, ...restVal } = val;
        return {...restVal };
    }
}

export { columns, buildTableData };